<template>
  <view class="goods_detail" v-if="goods_detail.goods_price">
    <!-- 轮播图 -->
    <swiper class="myswiper" :indicator-dots="true" :autoplay="true" :circular="true" :interval="3000" :duration="1000">
      <swiper-item v-for="(item, index) in goods_detail.pics" :key="index">
        <image @click="previewImsg(index)" class="myimg" :src="item.pics_big_url" mode="heightFix"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区别 -->
    <view class="info">
      <view class="price">
        ￥{{ goods_detail.goods_price }} -- {{ num }}
      </view>
      <view class="msg">
        <view class="left">
          {{goods_detail.goods_name }}
        </view>
        <view class="right">
          <uni-icons type="star" size="20"></uni-icons>
          收藏
        </view>
      </view>
      <view class="foot">
        快递：免运费1
      </view>
    </view>
    <!-- 商品介绍 -->
    <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    <!-- 商品导航区域 -->
    <uni-goods-nav class="goodsNav" :fill="true" :options="options" :buttonGroup="buttonGroup" @click="click"
      @buttonClick="buttonClick"></uni-goods-nav>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'
  export default {
    computed: {
      ...mapState('cart', ['num']),
      // 商品总数
      ...mapGetters('cart', ['total'])
    },
    // 侦听 total
    watch: {
      total: {
        handler(newV) {
          this.options[1].info = newV
        },
        immediate: true // 立即执行侦听器
      }
    },
    data() {
      return {
        // 商品详情对象
        goods_detail: {},
        // 商品 id
        goods_id: '',
        // 商品导航的左侧结构
        options: [{
            icon: 'shop',
            text: '店铺'
          },
          {
            icon: 'cart',
            text: '购物车',
            info: 0
          }
        ],
        // 右侧的按钮结构  
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        }, {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      };
    },
    onLoad(options) {
      // 接收商品 id
      this.goods_id = options.goods_id
      // 根据 id 得到商品详情
      this.getGoodsDetail()
    },
    methods: {
      ...mapMutations('cart', ['addToCart']),
      // 点击商品导航的右侧结构会触发
      buttonClick(e) {
        // 判断 是否点击的是加入购物车
        if (e.content.text === '加入购物车') {
          // 生成商品信息对象
          const goodsObj = {
            goods_id: this.goods_detail.goods_id,
            goods_name: this.goods_detail.goods_name,
            goods_price: this.goods_detail.goods_price,
            goods_small_logo: this.goods_detail.goods_small_logo,
            goods_count: 1,
            goods_status: true
          }
          // 将商品信息添加到购物车中
          this.addToCart(goodsObj)
          // 提示加入购物车成功
          uni.showToast({
            title: '加入购物车成功',
            icon: 'success'
          })
        }
      },
      // 点击商品导航的左侧结构会触发
      click(e) {
        if (e.content.text === '购物车') {
          // 跳转到购物车页面
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      // 预览图片
      previewImsg(index) {
        // 得到所有轮播图的图片集合
        const newArr = this.goods_detail.pics.map(item => {
          return item.pics_big_url
        })
        uni.previewImage({
          current: index,
          urls: newArr
        })
      },
      // 得到商品详情
      async getGoodsDetail() {
        const {
          data: res
        } = await uni.$http.get(`/api/public/v1/goods/detail?goods_id=${this.goods_id}`)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goods_detail = res.message
        // 将 goods_introduce 中的所有的 img 添加一个 style="display: block"
        this.goods_detail.goods_introduce = this.goods_detail.goods_introduce
          .replace(/<img /g, '<img style="display: block"')
          .replace(/webp/g, 'jpg')
      }
    }
  }
</script>

<style lang="scss">
  .goods_detail {
    margin-bottom: 50px;

    .myswiper {
      height: 750rpx;

      .myimg {
        height: 750rpx;
      }
    }

    .info {
      padding: 10px;

      .price {
        color: #c00000;
        font-size: 20px;
        line-height: 40px;
      }

      .msg {
        font-size: 14px;
        display: flex;
        align-items: center;

        .left {
          word-break: break-all;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          padding-right: 10px;
          border-right: 1px solid #ccc;
        }

        .right {
          width: 200rpx;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
        }
      }

      .foot {
        color: #b2b2b2;
        font-size: 14px;
        padding: 15px 0;
      }
    }

    .goodsNav {
      position: fixed;
      bottom: 0;
      left: 0;
      width: 100%;
    }
  }
</style>
